home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / GETINPUT.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  20KB  |  763 lines

  1. #include "pt.h"
  2. #include "string.h"
  3. #include "direct.h"
  4. #include "conio.h"
  5.  
  6. static unsigned char buffer[101];
  7. static int nameRow, nameCol;
  8. static int buttonState;
  9. static int space;
  10. static int maxNameSize;
  11.  
  12. unsigned char * pascal
  13. /* XTAG:getFileName */
  14. getFileName(prompt)
  15.     unsigned char *prompt;
  16. {
  17.     extern union REGS rin, rout;
  18.     extern unsigned char filePattern[];
  19.     extern unsigned char msgBuffer[];
  20.     extern unsigned char textBuffer[];
  21.     extern unsigned char fsPatterns[];
  22.     extern unsigned char fsDirs[];
  23.     extern unsigned char scratchFileName[];
  24.     extern int scrRows, scrCols;
  25.     extern unsigned char bannerColor;
  26.     extern unsigned char textColor, selColor;
  27.     extern unsigned char scrMapReset;
  28.     extern int debug;
  29.     extern int fsMenu;
  30.     extern int fileSort;
  31.     extern unsigned int dispMemory;
  32.     extern struct SREGS segRegs;
  33.     extern struct menuBlock far *menus[];
  34.     extern unsigned char startDirectory[];
  35.     extern unsigned char startDrive;
  36.     extern int helpMode;
  37.     extern unsigned char currentDirectory[];
  38.     extern unsigned char currentDrive;
  39.     extern int addHandle;
  40.     
  41.     unsigned char *fileName = NULL, *p, *q, *fileMarks;
  42.     unsigned char extension[3];
  43.     unsigned char far *fp;
  44.     unsigned int *ip;
  45.     int i, k, n, row, col, ret, iBuffer, fileCount;
  46.     int firstName, lastName;
  47.     int maxFiles, extensionLength;
  48.     int maxCol, offset;
  49.     int saveRow, saveCol;
  50.     int gettingDirectories;
  51.     int saveHelpMode;
  52.     struct SREGS sr;
  53.     int selectedRow, selectedCol;
  54.  
  55.     /* use the current directory that the user set last */
  56.     setDefaultDrive(currentDrive);
  57.     chdir(currentDirectory);
  58.  
  59.     /* set up so we know when to allocate space again */
  60.     space = 0;
  61.     saveHelpMode = helpMode;
  62.     helpMode = 0;
  63.     
  64. restart:
  65.     msg("***** Please wait while directory is read *****", 1);
  66.     
  67.     maxNameSize = 1;
  68.     maxFiles = 512;
  69.  
  70.     while( 1 ) {
  71.         /* get some space to put the names in */
  72.         if( space == 0 ) {
  73.             rin.h.ah = 0x48;
  74.             rin.x.bx = 4 * maxFiles;  /* 16-byte paragraphs */
  75.             intdos(&rin, &rout);
  76.             n = rout.x.cflag;
  77.             if( n & 0x1 ) {
  78.                 maxFiles >>= 1;    /* divide by 2 */
  79.                 if( maxFiles > 8 )
  80.                     continue;
  81.                 msg("No space to sort file names", 3);
  82.                 fileName = NULL;
  83.                 space = 0;
  84.                 goto askName;
  85.             }
  86.             space = rout.x.ax;
  87.         }
  88.         break;
  89.     }
  90.  
  91.     /* find the names */
  92.     fileCount = 0;
  93.  
  94.     /* first get all the directories */
  95.     gettingDirectories = 1;
  96.     fileName = findFirstPatternFile( "*.*", 0x10, buffer );
  97.  
  98.     while( fileCount < maxFiles ) {
  99.  
  100.         if( fileName == NULL ) {
  101. /* for some reason I can't figure out.  The directory search fails when */
  102. /* I remove this statement.  So I left it in. */
  103. if( debug==91 ) { setCPos(23,0);cprintf(" %d ", fileCount); }
  104.             /* are we done or just done with the directories? */
  105.             if( gettingDirectories ) {
  106.                 gettingDirectories = 0;
  107.                 fileName = findFirstPatternFile( filePattern,
  108.                             0x1, buffer );
  109.                 continue;
  110.             } else
  111.                 break;
  112.         }
  113.  
  114.         /* put in the string that defines the sort order (1st char) */
  115.         /* and marked the file type (2nd char) */
  116.         if( gettingDirectories ) {
  117.             if( (buffer[21]&0x10) != 0 )    /* a directory */
  118.                 /* directories will sort to the beginning */
  119.                 fileMarks = "DD";
  120.             else
  121.                 /* get only directories this pass */
  122.                 goto getNextFileName;
  123.         } else {
  124.             if( (buffer[21]&0x3) != 0 )
  125.                 /* hidden or read-only files */
  126.                 fileMarks = "ZH";
  127.             else
  128.                 fileMarks = "ZN";
  129.         }
  130.  
  131.         /* move the name into the buffer */
  132.         offset = 64 * fileCount;
  133.         /* put in the char to make directories sort in front */
  134.         movedata(segRegs.ds, (int)fileMarks, space, offset++, 1);
  135.  
  136.         if( fileSort == 2 ) {
  137.             /* find the extension */
  138.             q = NULL;
  139.             p = &buffer[30];
  140.             while( *p != '\0' ) {
  141.                 if( *p == '.' )
  142.                     q = p;
  143.                 ++p;
  144.             }
  145.             /* found a '.' that was not a ".."? */
  146.             i = 0;
  147.             if( q != NULL && buffer[30] != '.' ) {
  148.                 /* remove the extension from the name by */
  149.                 /* overlaying the '.' with end-of-string */
  150.                 *q++ = '\0';
  151.                 /* get the extension */
  152.                 while( *q != '\0' )
  153.                     extension[i++] = *q++;
  154.             }
  155.             /* pad extension with blanks */
  156.             extensionLength = i;
  157.             while( i < 3 )
  158.                 extension[i++] = ' ';
  159.             movedata(segRegs.ds, (int)(&extension[0]), space,
  160.                 offset, 3);
  161.             offset += 3;
  162.         }
  163.  
  164.         n = strlen(fileName) + 1;    /* + 1 for the '\0' */
  165.         if( fileSort == 2 && extensionLength > 0 ) {
  166.             n -= (extensionLength + 1);
  167.             /* +1 for the "." */
  168.             /* erase the extension from fileName */
  169.             fileName[n - 1] = '\0';
  170.         }
  171.         /* put in the file type chararacter */
  172.         fileName[n] = *(fileMarks + 1);
  173.         movedata( segRegs.ds, (int)fileName, space, offset, n+1 );
  174.         k = n + 1;    /* extra +1 for spacing on screen */
  175.         if( k > maxNameSize )
  176.             maxNameSize = k;
  177.         ++fileCount;
  178.  
  179. getNextFileName:
  180.         /* find the next matching file name */
  181.         fileName = findNextPatternFile(buffer);
  182.     }
  183.  
  184.     /* adjust for the size of the extension if sorting by extension */
  185.     if( fileSort == 2 )
  186.         maxNameSize += 4;
  187.     /* sort the names */
  188. if( fileSort != 0 ) {
  189.     for(k = 1; k < fileCount; k++) {
  190.         movedata(space, 64*k, segRegs.ds, (int)(&msgBuffer[0]),
  191.             maxNameSize);
  192.         i = k-1;
  193.         while( i >= 0 ) {
  194.             movedata(space, 64*i, segRegs.ds,
  195.                 (int)(&textBuffer[0]), maxNameSize);
  196.             if( strcmp(textBuffer, msgBuffer) <= 0 )
  197.                 break;
  198.             movedata(segRegs.ds, (int)(&textBuffer[0]),
  199.                 space, 64*(i+1), maxNameSize);
  200.             i--;
  201.         }
  202.         movedata(segRegs.ds, (int)(&msgBuffer[0]), space, 64*(i+1),
  203.             maxNameSize);
  204.     }
  205. }
  206.  
  207.     firstName = 0;
  208.     selectedRow = 0;
  209.     selectedCol = 0;
  210. doPaging:
  211.     /* clear the screen buffer where the names will go */
  212.     setMap(0, 0, scrRows-2, scrCols-1, 2, textColor);
  213.  
  214.     /* fill in the names */
  215.     n = firstName;
  216.  
  217.     /* maxNameSize has two blanks included in it so adjust for that */
  218.     maxCol = scrCols - (maxNameSize - 2);
  219.     for(col = 0; col < maxCol; col += maxNameSize) {
  220.         for(row = 2; row < scrRows-1; row++) {
  221.             if( n >= fileCount ) {
  222.                 lastName = fileCount;
  223.                 goto displayNames;
  224.             }
  225.             movedata(space, 64*n, segRegs.ds,
  226.                 (int)(&textBuffer[0]), 64);
  227.  
  228.             k = 0;    /* k counts screen character positions */
  229.             if( textBuffer[0] == 'D' )
  230.                 ret = selColor;    /* directory */
  231.             else
  232.                 ret = textColor;    /* ordinary file */
  233.  
  234.             /* is this filename selected? */
  235.             if( row == selectedRow && col == selectedCol )
  236.                 ret = selColor;        /* selected */
  237.             else
  238.                 ret = textColor;    /* not selected */
  239.  
  240.             i = (fileSort==2) ? 4 : 1;
  241.             for(  ; textBuffer[i] != '\0'; ++i)
  242.                 displayChar(row, col+k++,
  243.                     tolower(textBuffer[i]), ret);
  244.             iBuffer = i + 1;
  245.             if( fileSort == 2 && textBuffer[1] != ' ' ) {
  246.                 displayChar(row, col+k++, '.', ret);
  247.                 for(i = 1; i<4 && textBuffer[i]!=' '; i++)
  248.                     displayChar(row, col+k++,
  249.                         tolower(textBuffer[i]), ret);
  250.             }
  251.             if( textBuffer[iBuffer] == 'D' )
  252.                 displayChar(row, col+k, '\\', ret);
  253.             else if( textBuffer[iBuffer] == 'H' )
  254.                 displayChar(row, col+k, '*', ret);
  255.             ++n;
  256.         }
  257.     }
  258.     lastName = n;
  259.  
  260. displayNames:
  261.     /* remember these so that we can detect invalid click locations */
  262.     saveRow = row;
  263.     saveCol = col;
  264.     (void)getcwd(&msgBuffer[0], MSGBUFFERSIZE);
  265.     sprintf(textBuffer, "%d files in %s matching %s%s",
  266.         fileCount, msgBuffer, filePattern,
  267.         (fileCount < maxFiles ? "" : "SOME FILE NAMES MISSING")
  268.         );
  269.     col = 0;
  270.     for(i = 0; textBuffer[i] != '\0' && col < scrCols; i++)
  271.         displayChar(1, col++, textBuffer[i], bannerColor);
  272.     while( col < scrCols )
  273.         displayChar(1, col++, ' ', bannerColor);
  274.     pulldown(fsMenu);
  275.     updateScreen(0, scrRows-2);
  276.     fileName = getInput(prompt, "", 2);
  277.  
  278.     if( fileName != NULL )    /* A string was entered (no mouse button) */
  279.         goto processFilename;
  280.  
  281.     /* otherwise we got a mouse click and row and col are set */
  282.     /* normalize to first char in field */
  283.     if( nameRow > 0 ) {
  284.         /* normalize to first character in field */
  285.         nameCol = maxNameSize*(nameCol/maxNameSize);
  286.  
  287.         /* see if the click is where we put a file name */
  288.         if( (nameCol > saveCol) || (nameRow == scrRows-1)
  289.          || ((nameCol == saveCol) && (nameRow > saveRow)) ) {
  290.              /* if not, CD to the parent directory */
  291.              strcpy(scratchFileName, "..");
  292.             goto changeDirectory;